﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Business.Company;
using blackhouse.BlackApart.Business.Opinions;

namespace blackhouse.Aparts.Web.Model {
    public abstract class OpinionsModel {

        #region Fields

        private blackhouse.BlackApart.Business.Factory factory = blackhouse.BlackApart.Business.Factory.Instance();
        private StatisticsOpinion statistics;
        private ICompany company;

        #endregion

        #region Properties

        private StatisticsOpinion Statistics {
            get {
                if (this.statistics == null)
                    this.statistics = factory.GetStatisticsOpinion();
                return this.statistics;
            }
        }

        private ICompany Company {
            get {
                if (this.company == null)
                    this.company = factory.GetCompany();
                return this.company;
            }
        }

        #endregion

        public double GetAverageRating(int filterByApartment) {
            if (filterByApartment == 0)
                return this.Statistics.GetAverageRating();
            else
                return this.Statistics.GetAverageRating(filterByApartment);
        }

        public int GetBasedOnOpinions(int filterByApartment) {
            if (filterByApartment == 0)
                return this.Statistics.BasedOnNumberOfOpinions();
            else
                return this.Statistics.BasedOnNumberOfOpinions(filterByApartment);
        }

        public int GetStatisfactedClientsCount() {
            return this.Company.GetCounts().Clients;
        }

        public double GetSummaryCategoryValue(int filterByApartment, int categoryId) {
            if (filterByApartment > 0)
                this.statistics.SetFilterByApartmentType(filterByApartment);
            return Convert.ToDouble(this.Statistics.GetCategoryAverageRating(categoryId));
        }

        public IOpinionCategory[] GetOpinionCategories() {
            return this.factory.GetOpinionCategory().GetCategories();
        }

        public IOpinion[] GetOpinions(bool includeNonPublicOpinions) {
            return this.factory.GetOpinion().GetOpinions(includeNonPublicOpinions);
        }

        public IOpinion GetOpinion(int opinionId) {
            return this.factory.GetOpinion(opinionId);
        }

        public float GetSimilarAverageRating(int opinionId) {
            return this.Statistics.GetSimilarAverageRating(opinionId, TimeSpan.FromDays(90));
        }

        internal IOpinion[] GetSimilarOpinions(int opinionId) {
            return this.factory.GetOpinion().GetSimilarOpinions(opinionId, TimeSpan.FromDays(90));
        }

    }
}
